'Name:			oeLog.vb														##
'Author:		schachmat														##
'Last modified:	5.9.2007														##
'Status:																		##
'	functionality:		    90%													##
'	documentation intern:	0%													##
'	documentation extern:	0%													##
'Comments:																		##
'TODO: ComparExtend																##
'																				##
'																				##
'																				##
'																				##
'#################################################################################


'The MIT License

'Copyright (c) 2007 Markus Teich

'Permission is hereby granted, free of charge, to any person obtaining a copy
'of this software and associated documentation files (the "Software"), to deal
'in the Software without restriction, including without limitation the rights
'to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the Software is
'furnished to do so, subject to the following conditions:

'The above copyright notice and this permission notice shall be included in
'all copies or substantial portions of the Software.

'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
'IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
'THE SOFTWARE.

Imports OE
Imports OE.Core
Imports OE.Core.oeTools

Public Class oeLog
    Protected Shared LogFile As IO.FileStream
    Protected Shared SW As IO.StreamWriter

    Public Shared Sub InitLog(ByVal AppTitle As String)
        LogFile = New IO.FileStream(AppTitle & "-Log.html", IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.ReadWrite)
        SW = New IO.StreamWriter(LogFile, Text.Encoding.Default)
        SW.AutoFlush = False
        SW.WriteLine("<html><head><title>" & AppTitle & ": logfile</title></head>" _
        & "<body style=""font-family:Arial"">" _
        & "<h1>" & AppTitle & ": logfile</h1>" _
        & "<table style=""width: 100%; border-style:none;"">")
        Update(True)
    End Sub

    Public Shared Sub ExitLog()
        SW.WriteLine("</table></body></html>")
        Update(True)
        SW.Close()
        LogFile.Close()
    End Sub

    Public Shared Sub Update(ByVal ToFile As Boolean)
        SW.Flush()
        If ToFile Then
            LogFile.Flush()
        End If
    End Sub


    Public Shared Sub LogType(ByVal val As oeColor, Optional ByVal AddInfo As String = "")
        WriteLn("oeColor:", _
                Convert.ToString(IIf(AddInfo = "", "", AddInfo & "<br/>")) _
                & "<table style=""width: 100px; border: solid thin #C0C0C0"">" _
                & "<tr><td style=""width: 30px; border: solid thin #808080"" align=""center"">alpha</td>" _
                    & "<td style=""width: 70px; border: solid thin #808080; color:White; background-color:" _
                    & "#808080"">" & val.a.ToString & "</td></tr>" _
                & "<tr><td style=""width: 30px; border: solid thin #FF0000"" align=""center"">red</td>" _
                    & "<td style=""width: 70px; border: solid thin #FF0000; color:White; background-color:" _
                    & "#" & ByteToHex(val.red) & "0000"">" & val.r.ToString & "</td></tr>" _
                & "<tr><td style=""width: 30px; border: solid thin #00FF00"" align=""center"">green</td>" _
                    & "<td style=""width: 70px; border: solid thin #00FF00; color:White; background-color:" _
                    & "#00" & ByteToHex(val.green) & "00"">" & val.g.ToString & "</td></tr>" _
                & "<tr><td style=""width: 30px; border: solid thin #0000FF"" align=""center"">blue</td>" _
                    & "<td style=""width: 70px; border: solid thin #0000FF; color:White; background-color:" _
                    & "#0000" & ByteToHex(val.blue) & """>" & val.b.ToString & "</td></tr>" _
                & "</table>", New oeColor, False, False)
    End Sub

    Public Shared Sub LogType(ByVal val As oeMatrix, Optional ByVal AddInfo As String = "")
        WriteLn("oeMatrix:", _
                Convert.ToString(IIf(AddInfo = "", "", AddInfo & "<br/>")) _
                & "<table style=""width: 300px; border: solid thin #C0C0C0"">" _
                & "<tr><td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(0, 0).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(0, 1).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(0, 2).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(0, 3).ToString & "</td>" _
           & "</tr><tr><td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(1, 0).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(1, 1).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(1, 2).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(1, 3).ToString & "</td>" _
           & "</tr><tr><td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(2, 0).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(2, 1).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(2, 2).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(2, 3).ToString & "</td>" _
           & "</tr><tr><td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(3, 0).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(3, 1).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(3, 2).ToString & "</td>" _
                    & "<td style=""width: 75px; border: solid thin grey"" align=""center"">" & val.m0(3, 3).ToString & "</td>" _
                & "</tr></table>", New oeColor, False, False)
    End Sub

    Public Shared Sub LogType(ByVal val As oeVector3, Optional ByVal AddInfo As String = "")
        WriteLn("oeVector3:", _
                Convert.ToString(IIf(AddInfo = "", "", AddInfo & "<br/>")) _
                & "<table style=""width: 300px; border: solid thin #C0C0C0"">" _
                & "<tr><td style=""width: 30px; border: solid thin grey"" align=""center"">x</td>" _
                    & "<td style=""width:270px; border: solid thin grey"">" & val.x.ToString & "</td></tr>" _
                & "<tr><td style=""width: 30px; border: solid thin grey"" align=""center"">y</td>" _
                    & "<td style=""width:270px; border: solid thin grey"">" & val.y.ToString & "</td></tr>" _
                & "<tr><td style=""width: 30px; border: solid thin grey"" align=""center"">z</td>" _
                    & "<td style=""width:270px; border: solid thin grey"">" & val.z.ToString & "</td></tr>" _
                & "</table>", New oeColor, False, False)
    End Sub


    Public Shared Sub LogInfo(ByVal Text As String)
        WriteLn("Info:", Text, New oeColor(0.7, 0.7, 0), False, True)
    End Sub

    Public Shared Sub LogWarning(ByVal Text As String)
        WriteLn("WARNING:", Text, New oeColor(1.0, 0.5, 0), True, False)
    End Sub

    Public Shared Sub LogError(ByVal Text As String)
        WriteLn("ERROR:", Text, New oeColor(0.9, 0.0, 0), True, False)
    End Sub

    Public Shared Sub LogOk(ByVal Text As String)
        WriteLn("OK:", Text, New oeColor(0.0, 0.8, 0), True, False)
    End Sub

    Public Shared Sub LogText(ByVal Text As String, Optional ByVal Descr As String = "Textinfo:")
        WriteLn(Descr, Text, New oeColor(), False, False)
    End Sub


    Protected Shared Sub WriteLn(ByVal info As String, ByVal Line As String, ByVal cl As oeColor, _
                                 ByVal bold As Boolean, ByVal italic As Boolean)
        SW.WriteLine("<tr style=""color: #" & ByteToHex(cl.red) _
                                            & ByteToHex(cl.green) _
                                            & ByteToHex(cl.blue) _
                       & "; font-weight:" & Convert.ToString(IIf(bold, "bold", "normal")) _
                       & "; font-style: " & Convert.ToString(IIf(italic, "italic", "normal")) & """>" _
                & "<td style=""width:100px; border-right:black 2px solid; border-bottom:black 1px dashed;"">" _
                & info & "</td><td style=""border-bottom: black 1px dashed"">" _
                & Line & "</td></tr>")
    End Sub
End Class